<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="Name" prop="Name">
      <el-input v-model="ruleForm.Name"></el-input>
    </el-form-item>
<!--    <el-form-item label="Mold" prop="Mold">-->
<!--      <el-input v-model="ruleForm.Mold"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item
        label="Mold"
        prop="Mold"
        :rules="[
      { required: true, message: 'Mold不能为空'},
      { type: 'number', message: 'Mold必须为数字'}
    ]">
      <el-input type="Mold" v-model.number="ruleForm.Mold" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="MoldName" prop="MoldName">
      <el-input v-model="ruleForm.MoldName"></el-input>
    </el-form-item>
    <el-form-item label="Picture" prop="Picture">
      <el-input v-model="ruleForm.Picture"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        Name: '',
        Mold: '',
        MoldName: '',
        Picture: ''
      },
      rules: {
        Name: [
          { required: true, message: '请输入Name', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        Mold: [
          { min: 1, max: 3, message: '长度在 1 到 3 个数字', trigger: 'blur' }
        ],
        MoldName: [
          { required: true, message: '请输入MoldName', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var formData = new FormData();
          formData.append("name", this.ruleForm.Name);
          formData.append("mold", this.ruleForm.Mold);
          formData.append("moldName", this.ruleForm.MoldName);
          formData.append("picture", this.ruleForm.Picture);

          axios.post('http://10.10.10.78:3400/filed/addFiled', formData, {
            headers: { "Content-Type": "multipart/form-data" }
          }).then(function(resp){
            if (resp.data.code == 200){
              // _this.$message({
              //   message: '新增成功',
              //   type: 'success'
              // });
              _this.$alert('添加成功', '消息', {
                confirmButtonText: '确定',
                callback: action => {
                  _this.$router.push('/queryRoomInfo');
                }
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    if (localStorage.getItem("token") == null){
      this.$router.push('/');
      // window.location.reload()
    }
  }
}
</script>

<style scoped>

</style>